home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 003-desktop.lzm / usr / sbin / cups-genppdconfig.5.0 < prev    next >
Encoding:
Text File  |  2008-03-15  |  13.6 KB  |  481 lines

  1. #! /usr/bin/perl -w
  2. # $Id: cups-genppdconfig.in,v 1.13.8.1 2007/05/29 01:47:26 rlk Exp $
  3. # A user-friendly dialog-based wrapper for cups-genppd(8).
  4. # Copyright (C) 2002 Roger Leigh <rleigh@debian.org>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20. use strict;
  21. use File::Basename;
  22. use File::Find;
  23. use File::Temp qw(tempfile unlink0);
  24. use IO::Handle;
  25. use Getopt::Std;
  26. use POSIX;
  27.  
  28. sub init_data();
  29. sub init_defaults();
  30. sub main_menu();
  31. sub display_help;
  32. sub choose_printers;
  33. sub choose_languages;
  34. sub choose_location;
  35. sub create_ppds;
  36. sub create_dir($);
  37. sub dialog_read($$);
  38. sub dialog_read_list (\%\@$$);
  39.  
  40. my $DIALOG = "/usr/bin/dialog";                        # version of dialog to call
  41. my $BACKTITLE = "Gutenprint CUPS PPD creation"; # dialog screen title
  42. my %printers;                                   # master list of printers
  43. my %languages;                                  # master list of languages
  44. my @used_printers;                              # printer PPDs on system
  45. my @used_languages;                             # languages used on system
  46. my @chosen_printers = ();                       # chosen printers
  47. my @chosen_languages = ();                      # chosen languages
  48. my $version = "5.0";
  49. my $chosen_location = "/usr/share/cups/model/gutenprint/$version";
  50.                                                 # chosen PPD prefix
  51. my $silent = 0;                                 # no dialog
  52.  
  53.  
  54. # Set chosen_location from command-line.
  55. our $opt_d;
  56. our $opt_u;
  57. getopts('d:u');
  58. if ($opt_d) {
  59.     $chosen_location = create_dir($opt_d);
  60. }
  61.  
  62. # Initialise everything
  63. init_data();
  64. init_defaults();
  65.  
  66. # Run non-interactively if `-u' was specified.
  67. if ( $opt_u ) {
  68.     $silent = 1;
  69.     create_ppds;
  70.     exit 0;
  71. }
  72.  
  73. # Can we use dialog?
  74. die "dialog(1) is not available, but is required for interactive use."
  75.     if ! -x $DIALOG;
  76.  
  77. while (my $option = main_menu()) { # Display main menu and run selection
  78.     if ($option eq "Help") {
  79.     display_help();
  80.     } elsif ($option eq "Printers") {
  81.     choose_printers();
  82.     } elsif ($option eq "Languages") {
  83.     choose_languages();
  84.     } elsif ($option eq "Directory") {
  85.     choose_location();
  86.     } elsif ($option eq "Create") {
  87.     create_ppds();
  88.     } elsif ($option eq "Exit") {
  89.     exit 0;
  90.     } else {
  91.     die "Invalid menu option: $option";
  92.     }
  93. }
  94.  
  95. exit 0;
  96.  
  97.  
  98. #
  99. # init_data() - Populate master printer and language hashes.
  100. #
  101. sub init_data() {
  102.     my $model;
  103.     my $desc;
  104.     my $lang;
  105. # Get printer drivers and descriptions, then store in a hash.
  106.     open GENPPD, "cups-genppd.$version -M -v |" or die "can't fork cups-genppd.$version: $!";
  107.     while (<GENPPD>) {
  108.     ($model, $desc) = /([\w-]+)\W+(.*)/;
  109.     chomp ($model);
  110.     chomp ($desc);
  111.     $printers{$model} = $desc;
  112.     }
  113.     close GENPPD or die "can't close cups-genppd.$version pipe: $!";
  114. # Get available languages, then store in hash.
  115.     open GENPPD, "cups-genppd.$version -L |" or die "can't fork cups-genppd.$version: $!";
  116.     while (<GENPPD>) {
  117.     $lang = $_;
  118.     chomp ($lang);
  119.     $languages{$lang} = "(No description)";
  120.     }
  121.     $languages{"en"} = "US English";
  122.     close GENPPD or die "can't close cups-genppd.$version pipe: $!";
  123. # Set defaults
  124.     @chosen_languages = ("en");
  125. }
  126.  
  127.  
  128. #
  129. # init_defaults() - Get defaults from PPD files and directories.
  130. #
  131. sub init_defaults() {
  132.     # Find all PPD files that we could regenerate
  133.     my %found_ppds;
  134.     if (-d $chosen_location) {
  135.     find({wanted => \&find_printers}, $chosen_location);
  136.     foreach (@used_printers) {
  137.         my $tmp;
  138.         $tmp = basename($_);
  139.         chomp ($tmp);
  140.         $tmp =~ s/(^.*)\.ppd.*/$1/;
  141.         $found_ppds{$tmp} = "" if $printers{$tmp};
  142.     }
  143.     }
  144.     @chosen_printers = ();
  145.     foreach (sort keys %found_ppds) {
  146.     push @chosen_printers, $_;
  147.     }
  148.  
  149.     # Find all language directories that could be used
  150.     my %found_langs;
  151.     if (-d $chosen_location) {
  152.     find({wanted => \&find_languages}, $chosen_location);
  153.     foreach (@used_languages) {
  154.         my $tmp;
  155.         $tmp = basename($_);
  156.         chomp ($tmp);
  157.         $found_langs{$tmp} = "" if $languages{$tmp};
  158.     }
  159.     }
  160.     @chosen_languages = ();
  161.     foreach (sort keys %found_langs) {
  162.     push @chosen_languages, $_;
  163.     }
  164.     if (! @chosen_languages) {
  165.     push @chosen_languages, "en";
  166.     }
  167. }
  168.  
  169.  
  170. #
  171. # find-*() - callbacks for File::Find::find().
  172. #
  173. sub find_printers {
  174.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  175.  
  176.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  177.     -f _ &&
  178.     /^.*\.ppd.*\z/s && push @used_printers, $_;
  179. }
  180.  
  181. sub find_languages {
  182.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  183.  
  184.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  185.     -d _ && push @used_languages, $_;
  186. }
  187.  
  188.  
  189. # string
  190. # main_menu() - Display main menu.
  191. #               Return string containing selection.
  192. #
  193. sub main_menu() {
  194.     my $option;
  195.     my @menu_options;
  196.     my $menu_desc = << "END";
  197. Generate Gutenprint PPD files for use with CUPS.  This program is a user-friendly interface for cups-genppd(8).
  198.  
  199. Current PPD directory: $chosen_location
  200.  
  201. Hint: if the cursor keys cause problems, you may have more luck with +/- and TAB.
  202. END
  203.     @menu_options = (
  204.     [ "Help", "Display help text" ],
  205.     [ "Directory", "Choose PPD location" ],
  206.     [ "Printers", "Choose printers" ],
  207.     [ "Languages", "Choose languages" ],
  208.     [ "Create", "Create PPDs" ],
  209.     [ "Exit", "Exit the program" ]
  210.     );
  211.     my $dialog_options;
  212.     for my $i ( 0 .. $#menu_options) {
  213.     for my $j ( 0 .. $#{$menu_options[$i]}) {
  214.         $dialog_options .= "\"$menu_options[$i][$j]\" ";
  215.     }
  216.     }
  217.     while (defined($option = dialog_read("cups-genppdconfig", "--no-cancel --default-item Printers --menu \"$menu_desc\" 20 70 6 $dialog_options"))) {
  218.     chomp ($option);
  219.     return $option;
  220.     }
  221. }
  222.  
  223.  
  224. #
  225. # display_help() - Display help text.
  226. #
  227. sub display_help {
  228.     my $help_text = <<"END";
  229. cups-genppdconfig is a program to generate PPD files which enable the
  230. Gutenprint printer drivers to be used with CUPS, the Common UNIX
  231. Printing System.  A PPD file is a PostScript Printer Description,
  232. which describes the capabilities of a printer.  For each printer model
  233. that you wish to use, you will have to generate the corresponding PPD
  234. file.
  235.  
  236. There are three steps to generating the PPDs:
  237.  
  238.  
  239. [0. Directory]
  240.  
  241. The default base directory to create PPD files in is
  242. /usr/share/cups/model/gutenprint, and this is displayed on the
  243. main menu.  Choose the "Directory" option to change this, but in
  244. almost every case the default should be used.  Don\'t alter the default
  245. unless you know what you are doing.
  246.  
  247.  
  248. 1. Printers
  249.  
  250. Choose the "Printers" menu option.  The dialog box shows a complete
  251. list of all the printers supported by Gutenprint.  Use the up and down
  252. cursor keys to move between the printers and SPACE to select the
  253. models you want.  Next, press ENTER to confirm the selections.
  254.  
  255. If no printers are chosen, then a PPD file will be created for *every*
  256. model.
  257.  
  258.  
  259. 2. Languages
  260.  
  261. PPD files can be produced in several languages.  Choose the
  262. "Languages" menu option and, as for the "Printers" menu, select the
  263. languages that you wish to use and press ENTER to confirm the
  264. selections.
  265.  
  266. Since a PPD file can only be translated into one language, selecting
  267. multiple languages is posible, but of limited usefulness (for each
  268. printer, a separate PPD file for each language will be produced).
  269.  
  270.  
  271. 3. Save the selections
  272.  
  273. Choose the "Save" menu option to generate the PPD files you requested.
  274. The files will be created in the default CUPS data directory
  275. \$cups_prefix/share/model/gutenprint.  Translations will be saved in
  276. subdirectories named according to the locale/language.
  277.  
  278.  
  279. Note that unselecting already selected entries in the Printer and
  280. Language Selection dialogs will *not* remove these from the
  281. filesystem; it will simply not cause them to be generated when you
  282. choose "Save".  To remove a printer, delete the PPD file from each
  283. language directory it appears in.  To remove a language, delete the
  284. directory named with the corresponding language code, and all its
  285. contents.
  286.  
  287. Once you have finished, choose the "Exit" menu option, to leave the
  288. program.  Note that your selections will be lost, so make sure you
  289. saved your selections first, if you wanted to keep them.
  290. END
  291.     my($HELPFILE, $helpfilename) = tempfile("cups-genppdconfig-helpXXXXXX",
  292.                         UNLINK => 1)
  293.     or die "can't open temporary help file";
  294.     print $HELPFILE "$help_text";
  295.     $HELPFILE->flush();
  296.     dialog_read("Help", "--textbox $helpfilename 18 76");
  297.     unlink0($HELPFILE, $helpfilename) or die "Error unlinking help file $helpfilename safely: $!";
  298.     close($HELPFILE) or die "can't close help file $helpfilename: $!";
  299.     return;
  300. }
  301.  
  302.  
  303. #
  304. # choose_printers() - Select printers from master list.
  305. #                     Default none (so create all printers).
  306. #
  307. sub choose_printers {
  308.     my $title = "Printer selection";
  309.     my $options = "--checklist \"Choose the printer models you wish to use with CUPS.\" 18 54 11";
  310.     dialog_read_list(%printers, @chosen_printers, $title, $options);
  311. }
  312.  
  313.  
  314. #
  315. # choose_languages() - Select languages from master list.
  316. #                      Default is US English (en).
  317. #
  318. sub choose_languages {
  319.     my $title = "Language selection";
  320.     my $options = "--checklist \"Choose the languages you wish to use with CUPS.\" 18 54 11";
  321.     dialog_read_list(%languages, @chosen_languages, $title, $options);
  322. }
  323.  
  324.  
  325. #
  326. # choosen_location() - Select PPD prefix directory and create it if
  327. #                      not present.
  328. #
  329. sub choose_location {
  330.     my $location;
  331.     $location = dialog_read("Location selection",
  332.                 "--inputbox \"Choose a directory to create the PPD files in.\" 8 61 $chosen_location");
  333.     if (!defined($location)) {
  334.     $location = "";
  335.     }
  336.     $chosen_location = create_dir($location); # make sure directory exists
  337.     init_defaults; # use new location to get default selections
  338. }
  339.  
  340.  
  341. #
  342. # create_dir($dir) - Create named directory.
  343. #                    $dir will have excess `/'s pruned.
  344. #
  345. sub create_dir ($) {
  346.     my $location = $_[0];
  347.     my $dir;
  348.     my $count = 0;
  349.     if ($location =~ m/^\//) {
  350.     $dir = "/";
  351.     }
  352.     foreach (split /\//, $location) {
  353.     if ($_ eq "") {
  354.         next;
  355.     }
  356.     if ($count == 1) {
  357.         $dir .= "/";
  358.     }
  359.     $count = 1;
  360.     $dir .= $_;
  361.     if (!-d $dir) # directory does not exist, so create it
  362.     {
  363.         mkdir $dir || die "can't create directory \`$dir\': $!";
  364.     }
  365.     }
  366.     return $dir;
  367. }
  368.  
  369.  
  370. #
  371. # create_ppds() - Create PPD files.
  372. #
  373. sub create_ppds {
  374.     create_dir($chosen_location); # make the destination directory
  375.     my $total = scalar(@chosen_printers);
  376.     my $printers;
  377.     my $count;
  378.     my $language;
  379.     my $percent;
  380.     my $file;
  381.     if (!@chosen_printers) { # calculate total files for guage
  382.     $total = scalar(keys(%printers));
  383.     }
  384.     $total = $total * scalar(@chosen_languages);
  385.     if (@chosen_printers) { # construct printer list for dialog
  386.     foreach (@chosen_printers) {
  387.         $printers .= "$_ ";
  388.     }
  389.     } else {
  390.     $printers = "";
  391.     }
  392.     if (! $silent) {
  393.     open DIALOG, "| $DIALOG --sleep 2 --backtitle \"$BACKTITLE\" --title \"Creating PPD files\" --guage \"Language: \nPPD files: \" 10 72 0"
  394.         or die "can't fork dialog: $!";
  395.     }
  396.     $count = 0;
  397.     foreach $language (@chosen_languages) { # loop through languages
  398.     open GENPPD,
  399.     "LC_ALL=$language LANG=$language LANGUAGE=$language cups-genppd.$version -v -p $chosen_location/$language $printers 2>&1 |"
  400.         or die "can't fork cups-genppd: $!";
  401.     $file = "";
  402.     while ( defined($file = <GENPPD>)) { # dump genppd stats into guage
  403.         chomp($file);
  404.         $count++;
  405.         $percent = int (($count/$total)*100);
  406.         if ($percent > 100) {
  407.         $percent = 100;
  408.         }
  409.         if (! $silent) {
  410.         print DIALOG "$percent\n";
  411.         print DIALOG "XXX\nLanguage: $language\nPPD files: $count/$total\n\n$file\nXXX\n";
  412.         DIALOG->flush();
  413.         } else {
  414.         print "$file\n";
  415.         STDOUT->flush();
  416.         }
  417.     }
  418.     close GENPPD or die "can't close cups-genppd pipe: $!";
  419.     }
  420.     if (! $silent) {
  421.     print DIALOG "100\nXXX\nLanguage: \nPPD files: $total/$total\n\nCompleted\nXXX\n";
  422.     close DIALOG or die "can't close dialog pipe";
  423.     }
  424. }
  425.  
  426.  
  427. # scalar
  428. # dialog($title, $command) - Create a dialog.
  429. #                            Returns dialog results.
  430. sub dialog_read ($$) {
  431.     my($title, $command) = @_;
  432.     my $result = ""; # must not be undefined, just empty
  433.     my $status;
  434.     my $line;
  435.     open DIALOG, "$DIALOG --backtitle \"$BACKTITLE\" --title \"$title\" $command 2>&1 |";
  436.     while (defined($line = <DIALOG>)) {
  437.     $result .= $line;
  438.     }
  439.     close DIALOG or ($! == 0) or die "can't close dialog pipe: $!";
  440.     if ($? >> 8)
  441.     {
  442.     return undef;
  443.     }
  444.     return $result;
  445. }
  446.  
  447.  
  448. #
  449. # dialog_read_list(%masterlist
  450. #                  @chosenlist
  451. #                  $title
  452. #                  $dialog) - Construct list dialog, entries from %masterlist,
  453. #                             defaults from @chosenlist.
  454. #
  455. sub dialog_read_list (\%\@$$) {
  456.     my $masterlist = $_[0];
  457.     my $list = $_[1];
  458.     my $title = $_[2];
  459.     my $dialogoptions = $_[3];
  460.     my $tmplist;
  461.     my $dialoglist = "";
  462.     my $item;
  463.     my $selected;
  464. # Make a list for use with dialog.
  465.     foreach $item (sort keys %$masterlist) {
  466.     $selected = "off";
  467.     foreach (@$list) {
  468.         if ($item eq $_) {
  469.         $selected = "on";
  470.         last;
  471.         }
  472.     }
  473.     $dialoglist .= "$item \"$masterlist->{$item}\" $selected ";
  474.     }
  475.     $tmplist = dialog_read("$title", "$dialogoptions $dialoglist");
  476.     if (defined($tmplist)) {
  477.     $tmplist =~ s/\"//g;
  478.     @$list=split(/ /, $tmplist);
  479.     }
  480. }
  481.